package com.jtjrenren.android.taxi.driver.core.splash;

import android.content.Context;

import com.jtjrenren.android.taxi.driver.Constants;
import com.jtjrenren.android.taxi.driver.bean.event.DownLoadEvent;
import com.jtjrenren.android.taxi.driver.bean.api.ApiUpVersion;
import com.jtjrenren.android.taxi.driver.bean.event.UpEvent;
import com.jtjrenren.android.taxi.driver.bean.DriverInfo;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.library.mvp.presenter.MvpBasePresenter;
import com.library.utils.FileUtils;
import com.library.utils.SpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;


/**
 * Created by Administrator on 2016/8/17.
 */

public class SplashPresenterImpl extends MvpBasePresenter<SplashView, SplashInteractor> implements SplashPresenter {
    private static final String APK_PATH = FileUtils.getSDCardPath() + File.separator + "rrcx" + File.separator + "tmp";


    public SplashPresenterImpl(SplashView view) {
        super(view);
    }

    @Override
    public SplashInteractor createInteractor() {
        return new SplashInteractorImpl();
    }


    @Override
    public void checkAppVersion() {
        interactor.checkAppVersion();
    }

    @Override
    public void downApp(String url) {
        interactor.downApp(url, APK_PATH);
    }

    @Override
    public void checkLogin(Context context) {
        String driverID = (String) SpUtils.getParam(context, Constants.SP_DRIVER_REMBER, "");
        DriverInfo info = (DriverInfo) AppManger.getInstance().getAcacheByKey(driverID);

        if (info != null) {
            AppManger.getInstance().loginSuccess(context, info);
            view.splashFinish(SplashView.TYPE_Main);
        } else
            view.splashFinish(SplashView.TYPE_LOGIN);
    }

    @Override
    public void sub() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void unSub() {
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void handDownLoadEvent(DownLoadEvent downLoadEvent) {
        if (downLoadEvent.getType() == DownLoadEvent.TYPE_START) {

        } else if (downLoadEvent.getType() == DownLoadEvent.TYPE_DOWNING) {
            view.showDowning(downLoadEvent.getProcess());
        } else if (downLoadEvent.getType() == DownLoadEvent.TYPE_DOWNFINISH) {
            view.downFinish(downLoadEvent.getApk_path());
        } else if (downLoadEvent.getType() == DownLoadEvent.TYPE_ERROR) {
        }
    }

    @Subscribe
    public void handUpEvent(UpEvent event) {
        ApiUpVersion version = event.getApiUpVersion();
        if (version == null) {

            return;
        }
        if (version.getCode() == 1 && version.getData().size() > 0) {
            ApiUpVersion.VersionEntity versionEntity = version.getData().get(0);
            if (versionEntity.getIsUpdate() == 1)
                view.showUpAppTip(versionEntity, true);
            else
                view.showUpAppTip(versionEntity, false);
        } else
            view.showUpAppTip(null, true);
    }
}
